package com.rongke.web.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.commons.PageDto;
import com.rongke.model.User;
import com.rongke.model.UserAuthMsg;
import com.rongke.service.UserAuthMsgService;
import com.rongke.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @UserAuthMsgController
 * @用户认证Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/userAuthMsg")
@Transactional
@CrossOrigin
public class UserAuthMsgController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private UserAuthMsgService userAuthMsgService;
    @Autowired
    private UserService userService;

    /**
     * @添加用户认证
     * @param userAuthMsg
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addUserAuthMsg(@RequestBody UserAuthMsg userAuthMsg){
        log.debug("添加用户认证");
        userAuthMsg.setGmtDatetime(new Date());
        userAuthMsgService.insert(userAuthMsg);
        return JsonResp.ok(userAuthMsg);
    }

    /**
     * @修改用户认证
     * @param userAuthMsg
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateUserAuthMsg(@RequestBody UserAuthMsg userAuthMsg){
        log.debug("修改用户认证");

        if (userAuthMsg.getStatus()==1){
            UserAuthMsg userAuthMsg1=userAuthMsgService.selectById(userAuthMsg.getId());
            User user=userService.selectById(userAuthMsg1);
            user.setUserName(userAuthMsg1.getRealName());
            user.setAuthStatus(1);
            userService.updateById(user);
        }else if (userAuthMsg.getStatus()==0){
            UserAuthMsg userAuthMsg1=userAuthMsgService.selectById(userAuthMsg.getId());
            User user=userService.selectById(userAuthMsg1);
            user.setUserName(userAuthMsg1.getRealName());
            user.setAuthStatus(0);
            userService.updateById(user);
        }
        userAuthMsg.setUptDatetime(new Date());
        userAuthMsgService.updateById(userAuthMsg);
        return JsonResp.ok(userAuthMsg);
    }
    /**
     * @根据id查找用户认证
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectUserAuthMsg(Long id){
        log.debug("查找用户认证");
        UserAuthMsg userAuthMsg = userAuthMsgService.selectById(id);
        return JsonResp.ok(userAuthMsg);
    }

    /**
     * @根据用户认证信息
     * @param
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectLoginAuthMsg", method = RequestMethod.GET)
    public JsonResp selectLoginAuthMsg(){
        log.debug("查找登录用户认证信息");
        User loginUser=userService.findLoginUser();
        EntityWrapper<UserAuthMsg> ew = new EntityWrapper<>();
        ew.eq("user_id",loginUser.getId());
        UserAuthMsg one = userAuthMsgService.selectOne(ew);
        HashMap map=new HashMap();
        map.put("user",loginUser);
        if(one !=null){
            map.put("userAuthMsg",one);
        }
        return JsonResp.ok(map);
    }


    /**
     * 身份认证
     * @param
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/identityAuth", method = RequestMethod.POST)
    public JsonResp faceAuth(@RequestBody UserAuthMsg userAuthMsg, HttpServletRequest request) throws IOException {
        log.debug("身份认证");
        User loginUser = userService.findLoginUser();

        EntityWrapper<UserAuthMsg> iden = new EntityWrapper<>();
        iden.eq("id_card",userAuthMsg.getIdCard());
        UserAuthMsg one = userAuthMsgService.selectOne(iden);

        if (one != null && !loginUser.getId().equals(one.getUserId()) ) {
            return JsonResp.fa("该身份证号已被注册");
        }

        EntityWrapper<UserAuthMsg> ew = new EntityWrapper<>();
        ew.eq("user_id",loginUser.getId());
        UserAuthMsg identity = userAuthMsgService.selectOne(ew);
        if (identity == null) {
            userAuthMsg.setUserId(loginUser.getId());
            userAuthMsg.setGmtDatetime(new Date());
            userAuthMsgService.insert(userAuthMsg);
        }else{
            userAuthMsg.setId(identity.getId());
            userAuthMsg.setGmtDatetime(new Date());
            userAuthMsg.setUptDatetime(new Date());
            userAuthMsgService.updateById(userAuthMsg);
        }
        //改变认证状态
        loginUser.setAuthStatus(2);//待审核
        userService.updateById(loginUser);
        return JsonResp.ok("提交成功");
    }


    /***
     * 后台实名认证查询
     * */
    @RequestMapping(value = "/Findidentity", method = RequestMethod.GET)
    public JsonResp Findidentity(Page page,String idCard,String type){
        Map map=new HashMap();
        map.put("pageNo",(page.getCurrent()-1)*page.getSize());
        map.put("pageSize",page.getSize());
        map.put("idCard",idCard);
        if(type.equals("3")){
            map.put("type",null);
        }else{
            map.put("type",type);
        }
        List<UserAuthMsg>  UserAuthMsg=userAuthMsgService.Findidentity(map);
        Integer sun=userAuthMsgService.Findidentitycount(map);
        return JsonResp.ok(new PageDto(page.getCurrent(),page.getSize(),UserAuthMsg,sun));

    }



}
